#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          confirms that the smd -e options extends the
#          time of the job
############################################################################
# Copyright (C) 2011-2014 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
# All rights reserved
############################################################################
source ./globals

set file_in     "test$test_id.script"
set job_id      0
set node_name   ""
set time_limit  0
set exit_code   0

proc cleanup {} {
	global job_id node_name bin_rm file_in

	if {$job_id != 0} {
		cancel_job $job_id
	}
	if {$node_name ne ""}  {
		reset_state $node_name
	}

	exec $bin_rm -f $file_in
}

if {![param_contains [get_config_param "SlurmctldPlugstack"] "nonstop"]} {
	skip "This test is only compatible when SlurmctldPlugstack includes nonstop"
}
if {![is_super_user]} {
	skip "This test is only suitable for a super user (to restore down nodes)"
}

make_bash_script $file_in "$bin_sleep 100"

proc get_node {job_id} {
	global scontrol squeue re_word_str re_word_str

	set node_list ""
	spawn $squeue -j $job_id --noheader -o NodeList=%N
	expect {
		-re "NodeList=($re_word_str)" {
			set node_list $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "squeue is not responding"
		}
		eof {
			wait
		}
	}

	set node_name ""
	spawn $scontrol show hostnames $node_list
	expect {
		-re "($re_word_str)" {
			set node_name $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}
	return $node_name
}

proc reset_state {node} {
	global scontrol

	spawn $scontrol update NodeName=$node State=RESUME
	expect {
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}
}

proc get_time_limit {} {
	global smd time_limit number

	spawn $smd -c
	expect {
		-re "TimeLimitExtend: ($number)" {
			set time_limit $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "smd is not responding"
		}
		eof {
			wait
		}
	}
}

#
# Submit batch script to test
#
spawn $sbatch -N2 --no-kill --output=/dev/null $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	log_error "sbatch did not submit job"
	set exit_code 1
}

#
# Wait for job to start
#
if {[wait_for_job $job_id "RUNNING"] != 0} {
        cancel_job $job_id
        fail "Waiting for job to start"
}
set node_name [get_node $job_id]

#
# Drain node
#
set sub_match 0
spawn $smd -d $node_name $job_id -R test
expect {
	-re "Job $job_id node $node_name is being drained" {
		set sub_match 1
		exp_continue
	}
	timeout {
		fail "smd is not responding"
	}
	eof {
		wait
	}
}
if {$sub_match != 1} {
	log_error "smd did not drain node"
	set exit_code 1
}

#
# Replace node
#
set sub_match 0
spawn $smd -r $node_name $job_id
expect {
	-re "Job $job_id got node $node_name replaced" {
		set sub_match 1
		exp_continue
	}
	timeout {
		fail "smd is not responding"
	}
	eof {
		wait
	}
}
if {$sub_match != 1} {
	log_error "smd did not drain node"
	set exit_code 1
}

get_time_limit

#
# Extend the time for the job
#
set time_ext 0
spawn $smd -e [expr $time_limit - 1] $job_id
expect {
	-re "Job $job_id run time increased by [expr $time_limit - 1]\min" {
		set time_ext 1
		exp_continue
	}
	timeout {
		fail "smd is not responding"
	}
	eof {
		wait
	}
}

if {$time_ext != 1} {
	log_error "smd did not expend the timelimit"
	set exit_code 1
}

#
# Extend the time for the job past the limit
#
set time_ext 0
spawn $smd -e [expr $time_limit + 1] $job_id
expect {
	-re "failed job_id $job_id" {
		log_debug "This error is expected"
		set time_ext 1
		exp_continue
	}
	timeout {
		fail "smd is not responding"
	}
	eof {
		wait
	}
}
if {$time_ext != 1} {
	log_error "smd should have failed but did not"
	set exit_code 1
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
